import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/system/dictItem'

const api = {

    /**
     * 新增枚举项
     * @param dictItem
     * @returns {Promise<*|undefined>}
     */
    createDictItem: (dictItem) =>
      axiosClient().post(`${requestUrl}/addInfo`, dictItem),

    /**
     * 根据ID删除枚举项
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteDictItemById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新枚举项
     * @param id
     * @param dictItem
     * @returns {Promise<*|undefined>}
     */
    updateDictItemFull: (id, dictItem) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, dictItem),

    /**
     * 根据ID部分更新枚举项
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateDictItemPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询枚举项
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getDictItemById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询枚举项
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getDictItemPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询枚举项
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getDictItemList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.dictId) {
            params.append('dictId', businessParam.dictId)
        }
        if (businessParam.itemValue) {
            params.append('itemValue', businessParam.itemValue)
        }
        if (businessParam.itemLabel) {
            params.append('itemLabel', businessParam.itemLabel)
        }
        if (businessParam.itemColor) {
            params.append('itemColor', businessParam.itemColor)
        }
        if (businessParam.description) {
            params.append('description', businessParam.description)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
